﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Eniux.Windows.EnterpriseLibrary.Events
{
    /// <summary>
    /// 事件聚合类
    /// </summary>
    public class EventAggregator : IEventAggregator
    {
        #region 变量

        private readonly Dictionary<Type, EventBase> events = new Dictionary<Type, EventBase>();
        
        #endregion

        #region IEventAggregator接口相关

        public TEventType GetEvent<TEventType>() where TEventType : EventBase, new()
        {
            EventBase existingEvent = null;

            if (!this.events.TryGetValue(typeof(TEventType), out existingEvent))
            {
                TEventType newEvent = new TEventType();
                this.events[typeof(TEventType)] = newEvent;

                return newEvent;
            }
            else
            {
                return (TEventType)existingEvent;
            }
        } 

        #endregion
    }
}
